/**
 * 基础对象编辑器 - Date 时间编辑
 * 2018年11月16日 09点48分
 * nico@coser.pub
 */
module.exports._init = function () {
  this._DateEditor = function (base) {
    base.prototype.formatTo = function (formatString) {
      let result = "";

      let year = (this.getFullYear() + "").split("");
      let mouth = (this.getMonth() + 1 + "").split("");
      let day = (this.getDate() + "").split("");

      let hour = (this.getHours() + "").split("");
      let minute = (this.getMinutes() + "").split("");
      let second = (this.getSeconds() + "").split("");

      formatString = formatString;

      result = insertArrayFormatString("Y", formatString, year)
      result = insertArrayFormatString("M", result, mouth)
      result = insertArrayFormatString("D", result, day)

      result = insertArrayFormatString("h", result, hour)
      result = insertArrayFormatString("m", result, minute)
      result = insertArrayFormatString("s", result, second)
      
      return result;
    }
    /**
     * getDaysLengthFrom 获取从过去开始的天数 
     * @param Date pastDate 过去的时间
     * @result float
     */
    base.prototype.getDaysLengthFrom = function (pastDate) {
      return (this.valueOf() - pastDate.valueOf()) / (1000 * 60 * 60 * 24);
    }
    /**
     * getHoursLengthFrom 获取从过去开始的小时数
     * @param Date pastDate 过去的时间
     * @result float
     */
    base.prototype.getHoursLengthFrom = function (pastDate) {
      return (this.valueOf() - pastDate.valueOf()) / (1000 * 60 * 60);
    }
    /**
     * getMinutesLengthFrom 获取从过去开始的分钟数
     * @param Date pastDate 过去的时间
     * @result float
     */
    base.prototype.getMinutesLengthFrom = function (pastDate) {
      return (this.valueOf() - pastDate.valueOf()) / (1000 * 60 );
    }
    /**
     * getSecondsLengthFrom 获取从过去开始的秒数
     * @param Date pastDate 过去的时间
     * @result float
     */
    base.prototype.getSecondsLengthFrom = function (pastDate) {
      return (this.valueOf() - pastDate.valueOf()) / (1000 );
    }
  };
}


function insertArrayFormatString(field, formatString, array){
  array = array.reverse();
  let formatArray = formatString.split(field).reverse();
  if (formatArray.length == 1) return formatString;
  let result = formatArray[formatArray.length-1];
  for (let i = formatArray.length - 2; i > -1; i--){
    result += (array[i] || "0") + formatArray[i];
  }

  return result;
}